package org.base.oj.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.base.oj.common.redis.RedisCache;
import org.base.oj.domain.Question;
import org.base.oj.service.QuestionService;
import org.base.oj.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service
public class InitService {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private RedisCache redisCache;
    @Value("${GlobalValue.redisQuestionNumberKey}")
    private  String redisQuestionNumberKey;
    private Long initQuestionNumber = 10000L;
    @PostConstruct
    private void init() {
        //题号处理
        Page<Question> page = new Page<>(1, 1);
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Question::getCreateTime);
        List<Question> res = questionService.page(page, queryWrapper).getRecords();
        if (res != null && !res.isEmpty()){
            //获取最新题号
          initQuestionNumber =  CommonUtils.getValueFromString(res.get(0).getQuestionNumber());
        }
        redisCache.setCacheObject(redisQuestionNumberKey,initQuestionNumber);
    }

}
